package cc.vv.btpartyjob.jobactivities.fragment

import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.jobactivities.R
import cc.vv.btpartyjob.jobactivities.adapter.HeXinJiaZhiAdapter
import cc.vv.btpartyjob.jobactivities.delegate.HeXinJiaZhiFragmentDelegate
import cc.vv.btpartyjob.jobactivities.moudel.bean.HeXinJiaZhiBean
import cc.vv.btpartyjob.jobactivities.moudel.req.HeXinJiaZhiResponseObj
import cc.vv.btpartyjob.jobactivities.server.JobActivitiesServer
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 社会主义核心价值观fragment
 * Created by Chen.qi on 2018/10/30.
 */
class HeXinJiaZhiFragment : BaseFragmentMVP<HeXinJiaZhiFragmentDelegate>(),
        BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    /**
     * 适配器
     */
    private var mAdapter: HeXinJiaZhiAdapter? = null

    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<HeXinJiaZhiBean>? = null

    private var whetherRefresh = false

    /**
     * 状态  0 文明 1 和谐 2 敬业  3 友善 4 诚信
     */
    private var state: String = "0"

    fun setQueryState(state: String) {
        this.state = state
    }

    override fun initData() {
        if (mAdapter == null) {
            mDataList = ArrayList()
            mAdapter = HeXinJiaZhiAdapter(R.layout.item_data_list)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
    }

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (visible && !whetherRefresh) {
            pageNo = 1
            mDataList!!.clear()
            JobActivitiesServer.getSheHuiZhuYiJiaZhiGuanList(pageNo, pageSize, state, true)
            whetherRefresh = true
        }
    }


    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()
    }

    override fun getDelegateClass(): Class<HeXinJiaZhiFragmentDelegate> {
        return HeXinJiaZhiFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        if (whetherRefresh) {
            when (obj) {
                is HeXinJiaZhiResponseObj -> {
                    viewDelegate?.stopRefreshing()
                    if (null == obj.data) {
                        return
                    }
                    if (1 == pageNo) {
                        mDataList?.clear()
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        viewDelegate?.setAdapterData(mAdapter, mDataList)
                        mAdapter?.disableLoadMoreIfNotFullPage()
                    } else {
                        if (mAdapter?.data?.size!! >= obj.data.total) {
                            //数据全部加载完毕
                            mAdapter?.loadMoreEnd(true)
                            mAdapter?.loadMoreEnd()
                        } else {
                            //成功获取更多数据
                            if (null != obj.data.records) {
                                mDataList!!.addAll(obj.data.records)
                            }
                            mAdapter?.setNewData(mDataList)
                            //加载数据
                            mAdapter?.disableLoadMoreIfNotFullPage()
                            mAdapter?.loadMoreComplete()
                        }
                    }
                }
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        if (whetherRefresh) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreFail()
        }
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }

    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        JobActivitiesServer.getSheHuiZhuYiJiaZhiGuanList(pageNo, pageSize, state, true)


    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        JobActivitiesServer.getSheHuiZhuYiJiaZhiGuanList(pageNo, pageSize, state, true)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        JniStartActivity.startIntentWebActivity(activity, H5UrlEnum.核心价值观, mDataList?.get(position)?.id, " 详情")
    }
}